﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AssessmentActions;
using System.Web.Security;

public partial class Student_TakeATest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnTakeTest_Click(object sender, EventArgs e)
    {
        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;

        AssessmentDatabaseActions ada = new AssessmentDatabaseActions(strConnString);
        short parseResult = 0;
        if (Int16.TryParse(ddListTestNumbers.SelectedValue, out parseResult))
        {
            int testId = Int16.Parse(ddListTestNumbers.SelectedValue);
            try
            {
                string testString = ada.fetchTestHtmlWithSelectors(testId);
                fetchedTestLabel.Text = "<div style=\"background-color: #fff; border: 2px solid #777;\">" + testString + "</div>";
                btnSubmitTest.Visible = true;
            }
            catch
            {
                fetchedTestLabel.Text = "Invalid test number.";
            }
        }
        else
        {
            fetchedTestLabel.Text = "Invalid test number.";
        }
    }
    protected void btnSubmitTest_Click(object sender, EventArgs e)
    {
        string testId = Request.Form["assessmentId"];
        string testName = Request.Form["assessmentName"];

        string stringOfAnswers = "";

        int i = 0;
        foreach (string answer in Request.Form.Keys)
        {
            if (answer.IndexOf("tf_answers_") != -1)
            {
                //stringOfAnswers += "tf_answers_" + i + "-" + Request.Form[i] + "|";
                stringOfAnswers += Request.Form[i] + "|";
            }
            if (answer.IndexOf("mc_answers_") != -1)
            {
                //stringOfAnswers += "mc_answers_" + i + "-" + Request.Form[i] + "|";
                stringOfAnswers += Request.Form[i] + "|";
            }
            i++;
        }

        if (stringOfAnswers.Length > 0)
        {
            stringOfAnswers = stringOfAnswers.Remove(stringOfAnswers.Length - 1, 1);
        }

        MembershipUser myObject = Membership.GetUser();
        string UserID = myObject.ProviderUserKey.ToString();

        string userId = User.Identity.Name.ToString();
        int testIdAsInt = Int16.Parse(testId);

        EncryptionService.EncryptionServiceClient encrypter = new EncryptionService.EncryptionServiceClient();
        string encryptedAnswers = encrypter.Encrypt(stringOfAnswers, "secretPassword");

        AssessmentDatabaseActions ada = new AssessmentDatabaseActions(System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString);
        ada.postTestAnswers(testIdAsInt, UserID, encryptedAnswers, testName);

        fetchedTestLabel.Text = "Test Saved";
    }
}